using System;
using System.Windows.Forms;
using RollManager.Library;

namespace RollManager.Forms.DetailsPanels
{
    public partial class ActionDetailsPanel : UserControl, DetailsPanel
    {
        private readonly RollManager.Library.Action m_action;

        private string ActionName
        {
            get
            {
                return nameBox.Text.Trim();
            }
        }

        public ActionDetailsPanel( RollManager.Library.Action action )
        {
            InitializeComponent();

            // setup data fields
            m_action = action;
            nameBox.Text = m_action.Name;
            ownerLabel.Text = m_action.OwnerName;
            permissionsList.DataSource = Enum.GetValues( typeof( Permission ) );
            permissionsList.SelectedItem = m_action.Permission;

            // decide how to offer permissions adjusting
            if( m_action.UserOwned )
            {
                permissionsLabel.Visible = false;
            }
            else
            {
                if( m_action.Permission == Permission.Public )
                {
                    permissionsLabel.Text = "Public - changes are saved.";
                }
                else
                {
                    permissionsLabel.Text = "Read Only - changes are discarded.";
                }
                permissionsList.Visible = false;
            }
        }

        public void Save()
        {
            if( m_action.Saveable )
            {
                m_action.Name = ActionName;
                m_action.Permission = (Permission) permissionsList.SelectedValue;

                Database.SaveAction( m_action );
            }
        }
    }
}